package com.jprocms.module.cms.service.investigate;

import com.jpro.framework.common.pojo.PageResult;
import com.github.pagehelper.page.PageMethod;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionCreateReqVO;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionListReqVO;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionPageReqVO;
import com.jprocms.module.cms.controller.admin.investigateopinion.vo.InvestigateOpinionUpdateReqVO;
import com.jprocms.module.cms.convert.investigateopinion.InvestigateOpinionConvert;
import com.jprocms.module.cms.dal.dataobject.investigateitemop.InvestigateItemOpDO;
import com.jprocms.module.cms.dal.dataobject.investigateopinion.InvestigateOpinionDO;
import com.jprocms.module.cms.dal.dataobject.investigateoptionop.InvestigateOptionOpDO;
import com.jprocms.module.cms.dal.mysql.investigateitemop.InvestigateItemOpMapper;
import com.jprocms.module.cms.dal.mysql.investigateopinion.InvestigateOpinionMapper;
import com.jprocms.module.cms.dal.mysql.investigateoptionop.InvestigateOptionOpMapper;
import com.jprocms.module.cms.framework.seq.SnowflakeSequence;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static com.jpro.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.jprocms.module.cms.enums.ErrorCodeConstants.INVESTIGATE_OPINION_NOT_EXISTS;

/**
 * 问卷调查提交反馈 Service 实现类
 *
 * @author jprocms
 */
@Service
@Validated
public class InvestigateOpinionServiceImpl implements InvestigateOpinionService {

    @Resource
    private InvestigateOpinionMapper investigateOpinionMapper;
    @Resource
    private SnowflakeSequence snowflakeSequence;
    @Resource
    private InvestigateItemOpMapper itemMapper;
    @Resource
    private InvestigateOptionOpMapper optionMapper;

    @Transactional(rollbackFor = Exception.class)
    public void insert(InvestigateOpinionDO bean) {
        bean.setId(snowflakeSequence.nextId());
        investigateOpinionMapper.insert(bean);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertItem(InvestigateItemOpDO bean) {
        bean.setId(snowflakeSequence.nextId());
        itemMapper.insert(bean);
    }

    @Transactional(rollbackFor = Exception.class)
    public void insertOption(InvestigateOptionOpDO bean) {
        bean.setId(snowflakeSequence.nextId());
        optionMapper.insert(bean);
    }

    public boolean existsBy(@Nullable Long investigateId, @Nullable Date date,
                            @Nullable Long userId, @Nullable String ip, @Nullable Long cookie) {
        return PageMethod.offsetPage(0, 1, false).<Number>doSelectPage(() ->
                investigateOpinionMapper.countBy(investigateId, date, userId, ip, cookie)).iterator().next().intValue() > 0;
    }

    @Override
    public Long createInvestigateOpinion(InvestigateOpinionCreateReqVO createReqVO) {
        // 插入
        InvestigateOpinionDO investigateOpinion = InvestigateOpinionConvert.INSTANCE.convert(createReqVO);
        investigateOpinionMapper.insert(investigateOpinion);
        // 返回
        return investigateOpinion.getId();
    }

    @Override
    public void updateInvestigateOpinion(InvestigateOpinionUpdateReqVO updateReqVO) {
        // 校验存在
        validateInvestigateOpinionExists(updateReqVO.getId());
        // 更新
        InvestigateOpinionDO updateObj = InvestigateOpinionConvert.INSTANCE.convert(updateReqVO);
        investigateOpinionMapper.updateById(updateObj);
    }

    @Override
    public void deleteInvestigateOpinion(Long id) {
        // 校验存在
        validateInvestigateOpinionExists(id);
        // 删除
        investigateOpinionMapper.deleteById(id);
    }

    private void validateInvestigateOpinionExists(Long id) {
        if (investigateOpinionMapper.selectById(id) == null) {
            throw exception(INVESTIGATE_OPINION_NOT_EXISTS);
        }
    }

    @Override
    public InvestigateOpinionDO getInvestigateOpinion(Long id) {
        return investigateOpinionMapper.selectById(id);
    }

    @Override
    public List<InvestigateOpinionDO> getInvestigateOpinionList(Collection<Long> ids) {
        return investigateOpinionMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<InvestigateOpinionDO> getInvestigateOpinionPage(InvestigateOpinionPageReqVO pageReqVO) {
        return investigateOpinionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<InvestigateOpinionDO> getInvestigateOpinionList(InvestigateOpinionListReqVO exportReqVO) {
        return investigateOpinionMapper.selectList(exportReqVO);
    }

}
